package com.paipai.listen;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.paipai.service.LogService;
import com.rabbitmq.client.Channel;

@Component
public class LogListen {
	private Logger logger = LoggerFactory.getLogger(LogListen.class);
	
	@Autowired
	private LogService logService;

	@RabbitListener(queues = "paipai.log")
	@RabbitHandler
	public void duilie1(Message message, @Headers Map<String, Object> headers, Channel channel)
			throws UnsupportedEncodingException {
		System.out.println("收到日志消息!");
		// 获取消息的主体内容
		String msgBody = new String(message.getBody(), "UTF-8");
		@SuppressWarnings("unchecked")
		HashMap<String, Object> map = JSON.parseObject(msgBody, HashMap.class);

		System.out.println("paipai.log:" + map);
		System.out.println("写入日志!");
		logger.info("方法名类名:"+map.get("methods")+"描述:"+map.get("description")+"用户名:"+map.get("username"));
		//Integer row = logService.insertLog(map);
		//System.out.println(row);
		
	}

}
